//
//  HJDCTimeViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/5/23.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDCTimeViewController.h"
#import "HJDCTimeTableViewCell.h"
#import "HJDCTimeModel.h"
#import "HJDDetailTimeTableViewController.h"
#import "HJDCTimeViewController.h"

#import "AFNetworking.h"
#import "MBProgressHUD.h"
#import "MJExtension.h"

@interface HJDCTimeViewController ()<UITableViewDataSource,UITableViewDelegate>
{
//    相隔多少天
    int Days;
//    实际周数
    int shi;
}

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * CTimeArray;

@property (nonatomic,strong) NSMutableArray * indexArray;




@end

@implementation HJDCTimeViewController

-(NSMutableArray *)CTimeArray
{
    if (_CTimeArray ==nil) {
        
        _CTimeArray = [NSMutableArray array];
    }
    return _CTimeArray;
}

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:YES];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"产检记录";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor getColorWithHexNumber:@"848484"]}];

//     self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(shareBtnClick) image:@"矢量智能对象" HighImage:nil];
    self.indexArray = [[NSMutableArray alloc]init];
    
    [self setUpCell];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(popToPre) image:@"︿" HighImage:nil];
    
    NSString *phone = [[NSUserDefaults standardUserDefaults]stringForKey:@"phonenum"];
       NSString * url = @"/app/examinationList.do";
    NSString *path = [Hurl stringByAppendingString:url];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"]= phone;
 
    [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
       NSMutableArray *resultMap = dic[@"resultMap"];
        
        self.CTimeArray = (NSMutableArray *)[HJDCTimeModel mj_objectArrayWithKeyValuesArray:resultMap];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"服务器升级，请稍后再试"];
    }];
    

    
}

-(void)popToPre
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)setUpCell
{
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.CTimeArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    HJDCTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HJDCTimeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    HJDCTimeModel * model = self.CTimeArray[indexPath.row];
    
    NSData *sysData = [[NSUserDefaults standardUserDefaults]valueForKey:@"sysPregnancy"];
    NSString *sysStr = [NSKeyedUnarchiver unarchiveObjectWithData:sysData];
    if ([sysStr isKindOfClass:[NSNull class]]){
        NSUserDefaults *defaD = [NSUserDefaults standardUserDefaults];
        NSString *preD = [defaD objectForKey:@"pregnanc"];
        
        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [inputFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *inputDate = [inputFormatter dateFromString:preD];
        
        NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:inputDate];
        Days = ((int)time)/(3600*24 *7);
        
         shi = [model.weeks intValue];
        NSDate *nextDate = [NSDate dateWithTimeInterval:24*60*60 *7*shi sinceDate:inputDate];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *strDate = [dateFormatter stringFromDate:nextDate];

        cell.dateLbl.text = strDate;

    }else{
      
        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [inputFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *inputDate = [inputFormatter dateFromString:sysStr];
        
        NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:inputDate];
        Days = ((int)time)/(3600*24 *7);
         shi = [model.weeks intValue];
        NSDate *nextDate = [NSDate dateWithTimeInterval:24*60*60 *7*shi sinceDate:inputDate];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *strDate = [dateFormatter stringFromDate:nextDate];
        
        cell.dateLbl.text = strDate;

    }
    
    cell.numLbl.text = [NSString stringWithFormat:@"第%@次产检",model.index];
    cell.weekLbl.text = [NSString stringWithFormat:@"怀孕%@周",model.weeks];
    cell.stessLbl.text = @"重点";
//    防止背景颜色被覆盖，变成透明
//    cell.stessLbl.backgroundColor = [UIColor clearColor];
//    cell.stessLbl.layer.backgroundColor = [UIColor grayColor].CGColor;
    cell.projectLbl.text = model.emphasis;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    if (Days<=shi) {
        cell.btnImage.image = [UIImage imageNamed:@"选中"];
        cell.dateLbl.textColor = [UIColor getColorWithHexNumber:@"FF8395"];
        cell.stessLbl.backgroundColor = [UIColor clearColor];
        UIColor *back = [UIColor getColorWithHexNumber:@"FF8395"];
        cell.stessLbl.layer.backgroundColor = back.CGColor;
//        cell.stessLbl.backgroundColor = [UIColor getColorWithHexNumber:@"FF8395"];
    }else{
        cell.btnImage.image = [UIImage imageNamed:@"未选中"];
        cell.dateLbl.textColor = [UIColor getColorWithHexNumber:@"848484"];
        cell.stessLbl.backgroundColor = [UIColor clearColor];
        cell.stessLbl.layer.backgroundColor = [UIColor grayColor].CGColor;
        cell.projectLbl.textColor = [UIColor getColorWithHexNumber:@"BBBBBB"];
    }

    return cell;
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJDDetailTimeTableViewController *detail = [[HJDDetailTimeTableViewController alloc]init];
    HJDCTimeModel *model = self.CTimeArray[indexPath.row];
//    传值过去
    detail.ctimeModel = model;
    
    [self.navigationController pushViewController:detail animated:YES];

   }

@end
